package com.tarena.lbs.coupon.controller;

import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import com.tarena.lbs.coupon.service.UserCouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;

/**
 * 处理和优惠券数据业务有关的接口
 */
@RestController
@Slf4j
@Api(tags="优惠券模块")
public class CouponController {
    @Autowired
    CouponService  couponService;
    @Autowired
    UserCouponsService userCouponsService;


    @ApiOperation(value = "查询当前商家优惠券分页列表")
    @GetMapping("/admin/marketing/coupon/info/list")
    public Result<PageResult<CouponVO>> pageList( CouponQuery query) throws BusinessException{
        log.debug("查询优惠券列表,controller层");
        return new Result<>(couponService.pageList(query));
    }


    @ApiOperation(value = "新增优惠券")
    @PostMapping("/admin/marketing/coupon/info/save")
    public Result<Void> addCoupon(@RequestBody CouponParam couponParam)throws BusinessException{
        log.debug("添加优惠券,controller层");
        couponService.addCoupon(couponParam);
        return Result.success();
    }

    @ApiOperation(value = "查询优惠券详情")
    @GetMapping("/admin/marketing/coupon/info/detail/{id}")
    public Result<CouponVO> detail(@PathVariable("id") Integer id)
        throws BusinessException{
        log.debug("查询优惠券详情,controller层"+id);
        return new Result<>(couponService.detail(id));
    }


    @ApiOperation(value = "领取优惠券")
    @PostMapping("/marketing/user/receive/save")
    public Result<Void> receiveCoupon(@RequestBody UserCouponsParam param)
            throws BusinessException, ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();

        // 移除微服务认证逻辑，直接校验用户ID
        Asserts.isTrue(param.getUserId() == null,
                new BusinessException("-2", "用户ID不能为空"));
        log.info("用户领取优惠券，入参：{}", param);

        // 调用业务层领取
        userCouponsService.receiveCoupon(param);

        long end = System.currentTimeMillis();
        log.info("当前领取优惠券业务执行时间:{}MS", end - start);
        return Result.success();
    }


    @ApiOperation(value = "用户领取优惠券列表")
    @PostMapping("/marketing/user/receive/list")
    public Result<PageResult<UserCouponsVO>> receiveList(@RequestBody UserCouponQuery couponQuery)throws BusinessException{
        return new Result<>(userCouponsService.userCouponList(couponQuery));
    }


    @ApiOperation(value = "查看某优惠券码的二维码图片")
    @PostMapping("/marketing/user/receive/detail")
    public Result<UserCouponsVO> receiveDetail(@RequestBody UserCouponCodeQuery query)
            throws BusinessException{
        return new Result<>(userCouponsService.userCouponsDetail(query));
    }

}
